package xyz.caijiwang.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import xyz.caijiwang.entity.User;
import xyz.caijiwang.interceptor.TokenNotValidation;
import xyz.caijiwang.mapper.UserMapper;
import xyz.caijiwang.service.UserMsgService;
import xyz.caijiwang.utils.JwtUtil;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/user")
public class UserMsgController {

    @Autowired
    UserMsgService userMsgService;

    @Autowired
    UserMapper userMapper;

    @TokenNotValidation
    @GetMapping("/getUserHimself")
    public User getUserHimself(HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        Integer userid = JwtUtil.getUserId(token.substring(7));
        return userMsgService.selectByPrimaryKey(userid);
    }

    @TokenNotValidation
    @GetMapping("/getUserInfoNotImportant/{userid}")
    public User getUserInfoNotImportant(@PathVariable Integer userid) {
        User user = userMapper.selectByPrimaryKey(userid);
        user.setPassword(null);
        user.setPhone(null);
        user.setRoleid(null);
        return user;
    }

}
